#ifndef __PLATFORM_LIB_HASH_H__
#define __PLATFORM_LIB_HASH_H__
#ifdef __cplusplus
extern "C"
{
#endif

#include "pf_common.h"
#include "string.h"

/******************************************************************************************************/

typedef struct pf_hash pf_hash_t;

/*********************************************************************************************
Function Name:  pf_hash_create
Description  :  create a hash table
Inputs       :  uint32_t(*hash_value)(const void *key)             :   The hash arithmetic function
                int(*key_cmp)(const void *key1, const void *key2)  :   The key compare function
                uint32_t size                                      :   The hash table 's size
Outputs      :  return value                                       :   The pointer of the hash table
ErrorCodes   :  PF_ENOMEM                                          :   Out of memory
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern pf_hash_t* pf_hash_create(uint32_t (*hash_value)(const void *key),
                                 int (*keycmp)(const void *key1, const void *key2),
                                 uint32_t size);

/*********************************************************************************************
Function Name:  pf_hash_destory
Description  :  destroy a hash table
Inputs       :  pf_hash_t* hash                        :   The hash table
Outputs      :  return value                                :   Success or fail to destroy
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_hash_destory(pf_hash_t* hash);

/*********************************************************************************************
Function Name:  pf_hash_find_ptr
Description  :  find value by key
Inputs       :  pf_hash_t* hash                        :   The hash table
                void* key                                   :   The hash key
Outputs      :  return value                                :   The hash key 's value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_hash_find_ptr(const pf_hash_t* hash, void* key);

/*********************************************************************************************
Function Name:  pf_hash_find_num
Description  :  find value by key
Inputs       :  pf_hash_t* hash                        :   The hash table
                uint64_t key                                :   The hash key
Outputs      :  return value                                :   The hash key 's value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_hash_find_num(const pf_hash_t* hash, uint64_t key);

/*********************************************************************************************
Function Name:  pf_hash_insert_ptr
Description  :  insert a key-value pair
Inputs       :  pf_hash_t* hash                        :   The hash table
                void* key                                   :   The hash key
                void* value                                 :   The hash key 's value
Outputs      :  return value                                :   Success or fail to insert
ErrorCodes   :  PF_ENOMEM                                   :   Out of memory
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_hash_insert_ptr(pf_hash_t* hash, void* key, void* value);

/*********************************************************************************************
Function Name:  pf_hash_insert_num
Description  :  insert a key-value pair
Inputs       :  pf_hash_t* hash                        :   The hash table
                uint64_t key                                :   The hash key
                void* value                                 :   The hash key 's value
Outputs      :  return value                                :   Success or fail to insert
ErrorCodes   :  PF_ENOMEM                                   :   Out of memory
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_hash_insert_num(pf_hash_t* hash, uint64_t key, void* value);

/*********************************************************************************************
Function Name:  pf_hash_erase_ptr
Description  :  erase value by key
Inputs       :  pf_hash_t* hash                        :   The hash table
                void* key                                   :   The hash key
Outputs      :  return value                                :   The erased key 's value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_hash_erase_ptr(pf_hash_t* hash, void* key);

/*********************************************************************************************
Function Name:  pf_hash_erase_num
Description  :  erase value by key
Inputs       :  pf_hash_t* hash                        :   The hash table
                uint64_t key                                :   The hash key
Outputs      :  return value                                :   The erased key 's value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern void* pf_hash_erase_num(pf_hash_t* hash, uint64_t key);

/*********************************************************************************************
Function Name:  pf_hash_count
Description  :  get node count of a hash table
Inputs       :  pf_hash_t* hash                        :   The hash table
Outputs      :  return value                                :   The node count
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_count(const pf_hash_t* hash);

/*********************************************************************************************
Function Name:  pf_hash_clear
Description  :  erase all nodes of a hash table
Inputs       :  pf_hash_t* hash                        :   The hash table
Outputs      :  return value                                :   Success or fail to clear
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int pf_hash_clear(pf_hash_t* hash);

/*********************************************************************************************
Function Name:  pf_hash_for_each
Description  :  traverse a hash table and callback
Inputs       :  pf_hash_t* hash                        :   The hash table
                void* arg                                   :   callback function 's param
                int(*cb)(void* arg, void* key, void* value):  callback function
Outputs      :  return value                                :   Success or fail to clear
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern int32_t pf_hash_for_each(const pf_hash_t* hash,
                                void* arg,
                                int (*cb)(void* arg, void* key_ptr, uint64_t key_num, void* value));

/*********************************************************************************************
Function Name:  pf_hash_BKDR_str
Description  :  BKDR hash string function
Inputs       :  void* value                       :   The input string
Outputs      :  return value                      :   The hash value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_BKDR_str(const void* value);

/*********************************************************************************************
Function Name:  pf_hash_DJB_str
Description  :  DJB hash string function
Inputs       :  void* value                       :   The input string
Outputs      :  return value                      :   The hash value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_DJB_str(const void* value);

/*********************************************************************************************
Function Name:  pf_hash_SDBM_str
Description  :  SDBM hash string function
Inputs       :  void* value                       :   The input string
Outputs      :  return value                      :   The hash value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_SDBM_str(const void* value);

/*********************************************************************************************
Function Name:  pf_hash_PJW_str
Description  :  PJW hash string function
Inputs       :  void* value                       :   The input string
Outputs      :  return value                      :   The hash value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_PJW_str(const void* value);

/*********************************************************************************************
Function Name:  pf_hash_ELF_str
Description  :  ELF hash string function
Inputs       :  void* value                       :   The input string
Outputs      :  return value                      :   The hash value
ErrorCodes   :
History      :
---------------------------------------------------------------------------------------------
Author                 Date                          Comments
zhanggp                2011-06-29                    create
**********************************************************************************************/
extern uint32_t pf_hash_ELF_str(const void* value);

#if 0
/* serialization (maybe later) */
extern int32_t pf_hash_serialize(const pf_hash_t* hash,
                                 char* buffer,
                                 int32_t size);

extern pf_hash_t* pf_hash_deserialize(const char* data);
#endif

/******************************************************************************************************/

#ifdef __cplusplus
}
#endif
#endif /* #ifndef __PLATFORM_LIB_HASH_H__ */


